package com.cloud.util.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.cloud.portmana.entity.GlobalVariable;
import com.cloud.portmana.entity.dao.IBgGlobalVariableDao;

import config.SysConfig;

/**
 * Action父类，提供一些通用方法
 * 
 * @version 1.0
 * @date 2016-04-17
 * @author gaof
 */
public class BaseAction{
	
	/**
	 * 获取个人缓存信息
	 */
	protected UserInfoCache getUserInfoCache(HttpServletRequest request) {
		return (UserInfoCache) request.getSession().getAttribute(
				UserInfoCache.USER_INFO_SESSION_CACHE_KEY);
	}
	
	/**
	 * 获得个人账户信息
	 */
	protected IUserInfo getUserInfo(HttpServletRequest request) {

		UserInfoCache userInfoCache = getUserInfoCache(request);
		if (userInfoCache != null) {
			return userInfoCache.getUserInfo();
		}else{
			return null;
		}
		
		/*if(getUserInfoCache(ServletActionContext.getRequest())==null){
			return null;
		}
		
		return getUserInfoCache(ServletActionContext.getRequest()).getUserInfo();*/
		
	}
	@Resource(name="bgGlobalVariableDao")
	private IBgGlobalVariableDao bgGlobalVariableDao;
	/**
	 * 获取全局变量
	 */
	public Map<String,String> getGlobalVariableCache(HttpServletRequest request){
		Map<String,String> globalVariableCacheMap = (Map<String, String>) request.getSession().getAttribute(
				GlobalVariableCache.Global_Variable_SESSION_CACHE_KEY);
		if(globalVariableCacheMap != null){
			return globalVariableCacheMap;
		}else{
			globalVariableCacheMap = new HashMap<>();
			List<GlobalVariable> globalVariableList = bgGlobalVariableDao.getList();
			System.out.println(globalVariableList.size()+"--------");
			for(int i= 0;i<globalVariableList.size();i++){
				GlobalVariable globalVariable = globalVariableList.get(i);
				globalVariableCacheMap.put(globalVariable.getName(), globalVariable.getValue());
			}
			request.getSession().setAttribute(GlobalVariableCache.Global_Variable_SESSION_CACHE_KEY, globalVariableCacheMap);
			return globalVariableCacheMap;
		}
			
		
	}
	
	
	/**
	 * 获取系统后台页面路径
	 * @param path 格式：func/listFunc
	 * @return
	 */
	protected String getViewsModelPathSys(String path){
		return SysConfig.VIEWS_MODEL_PATH_SYS + path;
	}
	
	/**
	 * 获取门户网站页面路径
	 * @param path 格式：func/listFunc
	 * @return
	 */
	protected String getViewsModelPathPortals(String path){
		return SysConfig.VIEWS_MODEL_PATH_PORTALS + path;
	}
	
	/**
	 * 返回错误页面
	 * @return
	 */
	protected String getViewsModelPathSysError(){
		return SysConfig.VIEWS_MODEL_PATH_SYS_ERROR;
	}

}
